<template>
  <ele-modal
    width="460px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    title="创建文件夹"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="102px">
      <el-form-item label="文件夹名称:" prop="projectName">
        <el-input
          @input="onInput"
          clearable
          :maxlength="20"
          v-model="form.projectName"
          placeholder="请输入文件夹名称"
        />
      </el-form-item>
      <!--<el-form-item label="过期时间:" prop="expireDays" class="delInput">
        <el-input
          @input="onInput"
          clearable
          type="number"
          :maxlength="20"
          v-model.number="form.expireDays"
          placeholder="请输入过期时间"
        />
      </el-form-item>-->

      <!--<el-form-item label="公共文件夹:" prop="isCommon">
        <el-radio-group v-model="form.isCommon">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>-->

    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import {saveFolder} from '@/api/lncomingManagement/fileList';

  export default {
    props: {
      // 文件夹数据
      directorys: Array,
      // 弹窗是否打开
      visible: Boolean,
      // 父级 id
      parentId: Number,
    },
    data() {
      return {
        // 表单数据
        form: {
          projectName: '',
          isCommon: 0,
        },
        // 表单验证规则
        rules: {
          projectName: [
            {
              required: true,
              message: '请输入文件夹名称',
              trigger: 'blur'
            }
          ],
          expireDays: [
            {
              required: true,
              trigger: 'blur',
              pattern: /^([12][0-9]|30|[1-9])$/,
              message: '请输入1-30的数字',
            }
          ],
          isCommon: [
            {
              required: true,
              message: '请选择是否公共文件夹',
              trigger: 'blur'
            }
          ]
        },
        // 提交状态
        loading: false,
        types:0
      };
    },
    methods: {
      onInput(e){
        // forceUpdate用来强制渲染，避免data中对象层次太深，Vue框架不自动渲染的情况。
        this.$forceUpdate();
        console.log("输入框inputeeeeee",e)
      },
      show(type){
        // type: Number  // 1：自动成片页面  2：采集素材页面
        this.types=type
      },
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          Object.assign(this.form, {
            username: this.$store.state.user.info.username,
            parentId: this.parentId,
            merchantId: this.$store.state.user.info.merchantId,  // 商户id
            types:this.types
          })

          /*saveFolder({
            username: this.$store.state.user.info.username,
            parentId: this.parentId,
            projectName: this.form.projectName,
            isCommon: this.form.isCommon,
          })*/
            saveFolder(this.form)
            .then((data) => {
              this.loading = false;
              this.form.isCommon=0;
              console.log("成功", data)
              this.$message.success(data.msg);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              console.log("失败e", e)
              this.loading = false;
              this.$message.error(e);
            });
        });
      },
      /* 更新 visible */
      updateVisible(value) {
        this.$refs.form.resetFields()
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (!visible) {
          this.$refs.form.clearValidate();
          // this.form.projectName = '';
        }
      }
    }
  };
</script>
<style scoped>
  .v-width{
    width: 318px;
  }
  /*隐藏input（type=number)右边的小箭头*/
  /deep/.delInput input::-webkit-outer-spin-button,
  /deep/.delInput input::-webkit-inner-spin-button {
    -webkit-appearance: none!important;
  }
  /deep/.delInput input[type="number"]{
    -moz-appearance: textfield;
  }

</style>
